/**
 * 
 */
package org.vsg.common.jobscheduler.service.impl;

import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.vsg.common.configration.PropertiesSupport;
import org.vsg.common.jobscheduler.domain.QuartzTrigger;
import org.vsg.common.jobscheduler.service.QuartzService;

/**
 * @author vison
 *
 */
public class QuartzServiceImpl implements QuartzService {

	
	private SchedulerFactory schedulerFactory;
	
	@Resource
	private PropertiesSupport propertiesSupport;

	
	public SchedulerFactory getSchedulerFactory() {
		return schedulerFactory;
	}

	public void setSchedulerFactory(SchedulerFactory schedulerFactory) {
		this.schedulerFactory = schedulerFactory;
	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#definedJob(org.quartz.JobDetail, org.quartz.Trigger)
	 */
	@Override
	public void definedJob(JobDetail jobDetail, Trigger trigger) {
		// TODO Auto-generated method stub
		if (propertiesSupport != null) {
			Properties props = propertiesSupport.getProperties();
			JobDataMap  dataMap = jobDetail.getJobDataMap();
			if (dataMap == null) {
				dataMap = new JobDataMap();
			}
			dataMap.put("APPLICATION_SYSTEM_CONFIGRATION"  , props);
			jobDetail.setJobDataMap(dataMap);
		}
		
		try {
			schedulerFactory.getScheduler().scheduleJob(jobDetail, trigger);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#executeJobImmediatly(java.lang.String, java.lang.String)
	 */
	@Override
	public void executeJobImmediatly(String jobName, String groupName) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#findTriggerByGroup(java.lang.String)
	 */
	@Override
	public List<QuartzTrigger> findTriggerByGroup(String groupName) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#reDefinedJob(java.lang.String, java.lang.String, org.quartz.Trigger)
	 */
	@Override
	public void reDefinedJob(String jobName, String groupName, Trigger trigger) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#removeJob(java.lang.String, java.lang.String)
	 */
	@Override
	public void removeJobTrgger(String triggerName, String triggerGroup) {
		// TODO Auto-generated method stub
		
		try {
			schedulerFactory.getScheduler().unscheduleJob(triggerName, triggerGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.QuartzService#stopJob(java.lang.String, java.lang.String)
	 */
	@Override
	public void stopJobTrigger(String triggerName, String triggerGroup) {
		// TODO Auto-generated method stub
		
		try {
			schedulerFactory.getScheduler().pauseTrigger( triggerName , triggerGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void resumeJobTrigger(String triggerName, String triggerGroup) {
		// TODO Auto-generated method stub
		
		try {
			schedulerFactory.getScheduler().resumeTrigger(triggerName , triggerGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	
	
}
